package constellation.Activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.example.myapplication.R;
import com.google.gson.Gson;

import constellation.util.HttpUtils;
import constellation.util.LuckBean;
import constellation.util.ParnterBean;
import constellation.util.StartInfo;
import constellation.util.URLutils;

public class LuckDetailActivity extends AppCompatActivity {

    private ImageView imgBack;
    private TextView tvTitle;
    private TextView tvZonghe;
    private TextView tvAiqing;
    private TextView tvShiye;
    private TextView tvCaifu;
    private TextView tvJiankang;
    String parnterURL;
    Handler handler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what==1) {
                String json=(String)msg.obj;
                Gson gson=new Gson();
                LuckBean parnterBean=gson.fromJson(json,LuckBean.class);
                LuckBean.MimaBean mimaBean=parnterBean.getMima();

                tvZonghe.setText(mimaBean.getInfo()+"!!!"+mimaBean.getText()+"");
                tvAiqing.setText(parnterBean.getLove().get(0));
                tvShiye.setText(parnterBean.getCareer().get(0));
                tvCaifu.setText(parnterBean.getFinance().get(0));
                tvJiankang.setText(parnterBean.getHealth().get(0));
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_luck_detail);
        initView();
        String time=getIntent().getStringExtra("time");
        String xingzuo=getIntent().getStringExtra("xingzuo");
        tvTitle.setText(xingzuo);
        parnterURL =  URLutils.getLuckUrl(xingzuo,"year","a9b3b38d355a1ae425a4b962ec39602d","http://web.juhe.cn:8080/constellation/getAll");
        loadData();
    }
    public void  loadData(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                //执行网络请求
                String json= HttpUtils.getJsonByNetwork(parnterURL);
                //将子线程获取到的数据，通过handle对象传递到主线程
                Message msg=handler.obtainMessage();
                msg.what=1;
                msg.obj=json;
                handler.sendMessage(msg);
            }
        }).start();
    }
    private void initView() {
        imgBack = (ImageView) findViewById(R.id.img_back);
        imgBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tvTitle = (TextView) findViewById(R.id.tv_title);

        tvZonghe = (TextView) findViewById(R.id.tv_zonghe);
        tvAiqing = (TextView) findViewById(R.id.tv_aiqing);
        tvShiye = (TextView) findViewById(R.id.tv_shiye);
        tvCaifu = (TextView) findViewById(R.id.tv_caifu);
        tvJiankang = (TextView) findViewById(R.id.tv_jiankang);
    }
}
